package com.wjj.service;

import com.wjj.dao.repository.StudentRepository;
import com.wjj.dto.StudentDTO;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description
 * @Author junjun Wang
 * @Date Create in 0:06 2023/12/7
 **/
@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentRepository studentRepository;

	@Override
	public List<StudentDTO> getStudents() {
		return studentRepository.getStudents().stream()
				.map(StudentMapper::fromStudentPO)
				.collect(Collectors.toList());
	}

	@Override
	public StudentDTO getStudentById(Integer studentId) {
		return StudentMapper.fromStudentPO(studentRepository.getStudentById(studentId));
	}

	@Transactional
	@Override
	public Boolean addStudent(List<StudentDTO> students) {
		return studentRepository.addStudent(students.stream()
				.map(StudentMapper::toStudentPO).
				collect(Collectors.toList()));
	}

	@Transactional
	@Override
	public Integer updateStudent(StudentDTO student) {
		return studentRepository.updateStudent(StudentMapper.toStudentPO(student));
	}

	@Transactional
	@Override
	public Integer deleteStudentById(Integer studentId) {
		return studentRepository.deleteStudentById(studentId);
	}

	@Override
	public List<StudentDTO> getStudentByClassId(Integer classId) {
		return studentRepository.getStudentByClassId(classId).stream()
				.map(StudentMapper::fromStudentPO)
				.collect(Collectors.toList());
	}
}
